package com.jxinternet.platform.services.service.impl;

import com.jxinternet.platform.services.entity.domian.Message;
import com.jxinternet.platform.services.entity.domian.User;
import com.jxinternet.platform.services.entity.domian.Wealth;
import com.jxinternet.platform.services.entity.protobuf.RegisterProtos.RegisterRequest;
import com.jxinternet.platform.services.entity.protobuf.RegisterProtos.RegisterResponse;
import com.jxinternet.platform.services.protocol.BaseProtocol;
import com.jxinternet.platform.services.service.RegisterService;
import com.jxinternet.platform.services.service.impl.steps.RegisterSteps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by gold-jx on 2017/7/19.
 */
@Slf4j
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterSteps registerSteps;

    private Message message;

    @Override
    public RegisterResponse register(RegisterRequest registerRequest, String loginIp) {
        RegisterResponse.Builder builder = RegisterResponse.newBuilder();
        builder.setToken("");
        builder.setUserName("");
        //log.info("开始注册："+"RegisterRequest=["+registerRequest+"]");

        try {
            //1.验证请求
            message = registerSteps.step1_validateRequest(registerRequest);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            //2.验证用户
            String userName = registerRequest.getUserName();
            message= registerSteps.step2_validateUser(userName);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }


            //3.制造用户对象
            message = registerSteps.step3_makeUser(registerRequest);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            User user= (User) message.getData();

            //4.添加用户
            message = registerSteps.step4_addUser(user);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }


            //5.验证财富
            message = registerSteps.step5_validate_Wealth(userName);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            //6.财富对象制作
            message = registerSteps.step6_makeWealth(userName);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            Wealth wealth= (Wealth) message.getData();

            //7.添加财富
            message= registerSteps.step7_createWealth(wealth);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            //8.生成token
            message = registerSteps.step8_createToken(userName);
            String token= (String) message.getData();

            //9.生成response
            builder.setToken(token)
                    .setUserName(user.getUserName())
                    .setResultStatus(BaseProtocol.Data_Succeed_Code)
                    .setResultDescription(BaseProtocol.Data_Succeed_Des)
                    .build();
            //log.info("结束注册");

            //10.保存日志
            message=registerSteps.step9_saveLog(userName,loginIp);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            return builder.build();
        } catch (Exception e) {
            builder.setResultStatus(BaseProtocol.User_InvalidExcption_Code);
            builder.setResultDescription(BaseProtocol.User_InvalidExcption_Des);
            //log.error("注册失败",e);
            return builder.build();
        }
    }
}
